package ca.uwindsor;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;

public class EditMarks extends Activity {

	String course_code;

	// create an array adapter
		ArrayAdapter<CharSequence> adapter = null;
	
		Double existingWeight = 0.0;
		
	//TODO: get id from view marks
	int _id;
	public void set_id(int _id) {
		this._id = _id;
	}

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_marks);

		//get id from view schedule
				Intent intent = getIntent();
				set_id(intent.getExtras().getInt("_id"));
		
		

		Button add_but = (Button) findViewById(R.id.add_button);

		add_but.setText("Update");

		TextView lblMarks = (TextView)findViewById(R.id.lblmarks);
		lblMarks.setText("Edit Marks");
		
		
		// Alert Dialog builder
		final AlertDialog.Builder alert = new AlertDialog.Builder(this);

		
		//populate form values
		EditText marks = (EditText) findViewById(R.id.marks_ob);
		EditText totalMarks = (EditText) findViewById(R.id.total_mrk);
		EditText weight = (EditText) findViewById(R.id.weight);
		
		final MyDBAdapter db = new MyDBAdapter(getApplicationContext());
		db.setDATABASE_TABLE("marks"); //set database table

		db.open();
		
		
		
		Cursor marksCur = db.getEntry("_id", _id);
		if(marksCur.moveToFirst()){
			course_code = marksCur.getString(1);
		marks.setText(Double.toString(marksCur.getDouble(2)));
		totalMarks.setText(Double.toString(marksCur.getDouble(3)));
		weight.setText(Double.toString(marksCur.getDouble(4)));
		existingWeight = marksCur.getDouble(4);
		}
		
		// populate spinner
				fillSpinner();
				
		Spinner sCourseCode = (Spinner) findViewById(R.id.mrkSpinnerCode);
		
		sCourseCode.setOnItemSelectedListener(new MyOnItemSelectedListener());
		
		db.close();
		
		add_but.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				
				final MyDBAdapter db = new MyDBAdapter(getApplicationContext());
				db.setDATABASE_TABLE("marks"); //set database table

				db.open();

				EditText marks = (EditText) findViewById(R.id.marks_ob);
				EditText totalMarks = (EditText) findViewById(R.id.total_mrk);
				EditText weight = (EditText) findViewById(R.id.weight);
				//check if total weight of the course is greater than 100
				Cursor weightChk = db.getEntry("course_code",course_code);
				Double totalWeight = Double.parseDouble(weight.getText().toString());
				while(weightChk.moveToNext()){
					totalWeight += weightChk.getDouble(4);
				}
				totalWeight -= existingWeight; //minus existing marks
				if(marks.getEditableText().toString().trim().equals("") ||
						totalMarks.getEditableText().toString().trim().equals("") ||
						weight.getEditableText().toString().trim().equals(""))
						
				{
					alert.setMessage("All fields are required");
					alert.setNegativeButton("OK", new OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
						}

					});
					alert.show();
				}
				else if(Double.parseDouble(marks.getEditableText().toString()) >
				Double.parseDouble(totalMarks.getEditableText().toString()))
						
				{
					alert.setMessage("Total marks should be greater than marks ob.");
					alert.setNegativeButton("OK", new OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
						}

					});
					alert.show();
				}
				else if(totalWeight>100){
					//show alert to user
					alert.setMessage("Total weightage for this course is greater than 100.");

					alert.setNegativeButton("OK",
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									//do nothing
								}

							});
					alert.show();
				}
				else{
					//add to database
				Double dMarks = Double.parseDouble(marks.getText().toString());
				Double dTotalMarks = Double.parseDouble(totalMarks.getText().toString());
				Double dWeight = Double.parseDouble(weight.getText().toString());
				Double cal = (dMarks/dTotalMarks) * dWeight;
				//add to database
				ContentValues newValues = new ContentValues();

				newValues.put("course_code", course_code);

				newValues.put("marks", marks.getText().toString());

				newValues.put("total_marks", totalMarks.getText().toString());

				newValues.put("weightage", weight.getText().toString());
				
				newValues.put("marks_ob", cal.toString());

				db.updateEntry("_id", _id, newValues);

				alert.setMessage("Marks added for course " + course_code);

				alert.setNegativeButton("OK",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								startActivity(new Intent("ca.uwindsor.VIEWMARKS"));
							}

						});
				alert.setCancelable(false);
				alert.show();
				}
				db.close();
			}

		});
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}

	public void fillSpinner() {
		// create an array adapter
		adapter = new ArrayAdapter<CharSequence>(
				this, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		adapter.add(course_code);
		
		Spinner s = (Spinner) findViewById(R.id.mrkSpinnerCode);
		s.setAdapter(adapter);

	}

	public class MyOnItemSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			// System.out.print(parent.getAdapter().toString());
			switch (parent.getId()) {
			case R.id.mrkSpinnerCode:
				course_code = parent.getItemAtPosition(pos).toString();
				break;

			}

		}

		public void onNothingSelected(AdapterView<?> parent) {
			// Do nothing.
		}
	}
}
